package com.deer.wms.instock.service.enter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.service.AsnMasterService;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.deer.wms.project.root.util.CollectionUtils.convertMap;

/**
 * @Author: xu
 * @Date: 2022/06/20/10:31
 * @Description:
 */
public abstract class AbstractEnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private AsnMasterService asnMasterService;

    //获取类型
    protected abstract ErpType gainType();

    /**
     * 字段映射
     * @param asnMaster
     * @param asnDetails
     * @param
     */
    public OrderToErpBO EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails) {
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(asnMaster.getAsnType());
        orderToErpBO.setSourceBillTypeId(asnMaster.getBillType());
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("INPUT"));
        for (AsnDetail asnDetail : asnDetails) {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            AsnMaster master = asnMasterService.getOne(new QueryWrapper<AsnMaster>().eq("bill_no", asnDetail.getBillNo()));
            orderDetailBO.setProductionDate(DateUtils.getNowDateString());
            orderDetailBO.setSourceBillId(master.getBeforeNum()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(asnDetail.getErpLineNo()); //行号
            orderDetailBO.setBatchNo(asnDetail.getBatchName()); //批号
            orderDetailBO.setQty(asnDetail.getQty()); //数量
            orderDetailBO.setStockId(asnDetail.getStockId()); //
            orderDetailBO.setDetailNo(asnDetail.getDetailNo());
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);
        return orderToErpBO;
    }

    /**
     * 字段添加
     *  @param orderToErpBO
     * @param asnMaster
     * @param asnMap
     */
    protected abstract void add(OrderToErpBO orderToErpBO, AsnMaster asnMaster, Map<String, AsnDetail> asnMap);

    /**
     * 回传ERP
     * @param orderToErpBO
     */
    protected abstract void pushErp(OrderToErpBO orderToErpBO);


    /**
     * 方法汇总
     * @param asnMaster
     * @param asnDetails
     * @param enterAssist
     */
    public void sendERP(AsnMaster asnMaster, List<AsnDetail> asnDetails) {

        //Step1.抽象类实现字段映射
        OrderToErpBO orderToErpBO = this.EnterBeanAndPush(asnMaster, asnDetails);
        //Step2.子类实现特殊字段添加
        Map<String, AsnDetail> asnMap = convertMap(asnDetails, AsnDetail::getDetailNo);
        this.add(orderToErpBO, asnMaster, asnMap);
        //Step3. 子类实现回传ERP
        this.pushErp(orderToErpBO);
    }

}
